package com.xsherl.leetcode.base;

import java.util.ArrayList;
import java.util.List;

public class ListNode {
    public int val;
    public ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public static ListNode build(int... vals) {
        ListNode root = null;
        for (int i = vals.length - 1; i >= 0; i--) {
            ListNode node = new ListNode(vals[i], root);
            root = node;
        }
        return root;
    }

    public static ListNode[] build(List<int[]> vals) {
        List<ListNode> nodes = new ArrayList<>();
        for (int[] m : vals) {
            nodes.add(build(m));
        }
        return nodes.toArray(new ListNode[0]);
    }

    public boolean equals(ListNode other){
        ListNode root = this;
        while (root != null && other != null){
            if (root.val != other.val){
                return false;
            }
            root = root.next;
            other = other.next;
        }
        return root == null && other == null;
    }

    @Override
    public String toString() {
        return val + " -> " + next;
    }
}
